/*
 * StateMachineValueDisplay.java
 *
 * Created on 18 March 2004, 09:28
 * Copyright (C) 2004, Velocityme Partnership
 */

package com.velocityme.client.gui.node.taskstatemachine;

import com.velocityme.client.gui.ListAssignPanel;
import com.velocityme.valueobjects.RoleValue;
import com.velocityme.valueobjects.StateValue;
import com.velocityme.valueobjects.StatusValue;
import com.velocityme.valueobjects.TransitionValue;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;

/**
 *
 * @author  Robert
 */
public class StateMachineValueDisplay extends javax.swing.JPanel {
    
    private TransitionTableModel m_modelTransitions;
    private ListAssignPanel m_listAssignPanel;
    private Collection m_originalTransitions = new ArrayList();
    private int m_selectedRow;
    
    /** Creates new form StateMachineValueDisplay */
    public StateMachineValueDisplay(TransitionValue[] p_transitionValues, StateValue p_startStateValue, StateValue[] p_allStateValues, StatusValue[] p_allStatusValues, RoleValue[] p_allRoleValues) {
        initComponents();
        java.awt.GridBagConstraints gridBagConstraints;
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.gridx = 1;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 0.25;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 0);
        Collection allRoleValues = new ArrayList();
        for(int i = 0; i < p_allRoleValues.length; i++)
            allRoleValues.add(p_allRoleValues[i]);
        add(m_listAssignPanel = new ListAssignPanel(allRoleValues), gridBagConstraints);
        setTransitionValueObjects(p_transitionValues, p_startStateValue, p_allStateValues, p_allStatusValues, p_allRoleValues);
        for(int i = 0; i < p_transitionValues.length; i++)
            m_originalTransitions.add(p_transitionValues[i]);
    }
    
    public void setTransitionValueObjects(TransitionValue[] p_transitionValues, StateValue p_startStateValue, StateValue[] p_allStateValues, StatusValue[] p_allStatusValues, RoleValue[] p_allRoleValues) {
        StateValue[] allButStartStateValues = new StateValue[p_allStateValues.length-1];
        for (int i = 0, j = 0; i < p_allStateValues.length; i++)
            if (!p_allStateValues[i].equals(p_startStateValue))
                allButStartStateValues[j++] = p_allStateValues[i];
        jTableTransitions.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(new JComboBox(allButStartStateValues)));
        jTableTransitions.getColumnModel().getColumn(1).setCellEditor(new DefaultCellEditor(new JComboBox(p_allStateValues)));
        jTableTransitions.setDefaultEditor(StatusValue.class, new DefaultCellEditor(new JComboBox(p_allStatusValues)));
        jTableTransitions.getColumnModel().getColumn(5).setCellEditor(new RoleEditor(m_listAssignPanel));
        m_modelTransitions.setTransitionValues(p_transitionValues);
        m_modelTransitions.setEditable(true, p_startStateValue, p_allStateValues[p_allStateValues.length-1], p_allStatusValues[0]);
    }
    
    public TransitionValue[] getTransitionValueObjects() {
        return m_modelTransitions.getTransitionValues();
    }
    
    private void checkPopup(java.awt.event.MouseEvent e) {
        if (e.isPopupTrigger()) {
            m_selectedRow = jTableTransitions.getSelectedRow();
            jMenuItemDelete.setEnabled(true);
            jMenuItemAdd.setEnabled(true);
            if (m_selectedRow < 0 || m_modelTransitions.getTransitionAt(m_selectedRow).getSourceStateValue().getNodeValue().getName().equals("Start State")) {
                jMenuItemDelete.setEnabled(false);
            }
            jPopupMenu1.show(e.getComponent(), e.getX(), e.getY());
        }
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        java.awt.GridBagConstraints gridBagConstraints;

        jPopupMenu1 = new javax.swing.JPopupMenu();
        jMenuItemAdd = new javax.swing.JMenuItem();
        jMenuItemDelete = new javax.swing.JMenuItem();
        jLabelTransitions = new javax.swing.JLabel();
        jScrollPaneTransitions = new javax.swing.JScrollPane();
        m_modelTransitions = new TransitionTableModel();
        jTableTransitions = new javax.swing.JTable(m_modelTransitions);
        jLabelResponsibleRoles = new javax.swing.JLabel();

        jMenuItemAdd.setText("Add");
        jMenuItemAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemAddActionPerformed(evt);
            }
        });

        jPopupMenu1.add(jMenuItemAdd);

        jMenuItemDelete.setText("Delete");
        jMenuItemDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemDeleteActionPerformed(evt);
            }
        });

        jPopupMenu1.add(jMenuItemDelete);

        setLayout(new java.awt.GridBagLayout());

        jLabelTransitions.setText("Transitions:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        add(jLabelTransitions, gridBagConstraints);

        jTableTransitions.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTableTransitionsMouseClicked(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jTableTransitionsMousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jTableTransitionsMouseReleased(evt);
            }
        });

        jScrollPaneTransitions.setViewportView(jTableTransitions);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 0.5;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        add(jScrollPaneTransitions, gridBagConstraints);

        jLabelResponsibleRoles.setText("Responsible Roles:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        add(jLabelResponsibleRoles, gridBagConstraints);

    }//GEN-END:initComponents

    private void jTableTransitionsMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableTransitionsMouseReleased
        // Add your handling code here:
        checkPopup(evt);                
    }//GEN-LAST:event_jTableTransitionsMouseReleased

    private void jTableTransitionsMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableTransitionsMousePressed
        // Add your handling code here:
        checkPopup(evt);
    }//GEN-LAST:event_jTableTransitionsMousePressed

    private void jTableTransitionsMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableTransitionsMouseClicked
        // Add your handling code here:
        checkPopup(evt);        
    }//GEN-LAST:event_jTableTransitionsMouseClicked

    private void jMenuItemDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemDeleteActionPerformed
        // Add your handling code here:
        m_modelTransitions.removeRows(m_selectedRow,m_selectedRow);        
    }//GEN-LAST:event_jMenuItemDeleteActionPerformed

    private void jMenuItemAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemAddActionPerformed
        // Add your handling code here:
        m_modelTransitions.addValueAt(m_modelTransitions.getRowCount());
    }//GEN-LAST:event_jMenuItemAddActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabelResponsibleRoles;
    private javax.swing.JLabel jLabelTransitions;
    private javax.swing.JMenuItem jMenuItemAdd;
    private javax.swing.JMenuItem jMenuItemDelete;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JScrollPane jScrollPaneTransitions;
    private javax.swing.JTable jTableTransitions;
    // End of variables declaration//GEN-END:variables
    
}
